<?php
class Testwx extends BZ_Controller
{
    public function _init()
    {
        $this->load->driver('cache');
        $this->load->helper(array('validation', 'api', 'common'));
        $this->load->library('alipay/authorization', null, 'authorization');
        $this->load->model('scenery_base_model', 'scenery_base');
        $this->load->model('goods_base_model', 'goods_base');
        $this->load->model('scenery_reviews_model', 'scenery_reviews');
        $this->load->model('order_base_model', 'order_base');
        $this->load->model('pay_record_model', 'pay_record');
        $this->load->model('order_profit_model', 'order_profit');
        $this->load->model('order_source_model', 'order_source');
        $this->load->model('make_code_model', 'make_code');
        $this->load->model('user_account_model', 'user_account');
        $this->load->model('getpwd_phone_model', 'getpwd_phone');
        $this->load->model('goods_map_profit_model', 'goods_map_profit');
        $this->load->model('user_model', 'user');
        $this->load->model('tourism_goods_base_model', 'tourism_goods_base');
        $this->load->model('bind_user_model', 'bind_user');
        $this->load->model('scan_recommend_products_model', 'scan_recommend_products');
        $this->load->model('region_model', 'region');
        $this->load->model('pay_record_model','pay_record');
    }
    
    public function index($payId)
    {
        $result = $this->bind_user->findByOpenOrAli($this->uid);
        if ($result->num_rows() <= 0) {
            return;
        }
        $bindUser = $result->row(0);
        $openId = $bindUser->open_id;
        
        $result1 = $this->pay_record->findByPayId($payId);
        if ($result1->num_rows() <= 0) {
            return ;
        }
        $payRecord = $result1->row(0);
        include_once(APPPATH.'libraries/wxpay/WxPayPubHelper/WxPayPubHelper.php');
        $jsapi = new JsApi_pub();
        $accessToken = $jsapi->getAccessToken();
        if (isset($accessToken->errcode)) {
            return $accessToken->errmsg;
        }
        $urlOne = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$accessToken->access_token;
        $data['touser']           = $openId;
        $data['template_id']      = 'zFSPosWPThOgUiZcu7ywOBfHgikG6n-Sj1slBhyVQqw';
        $data['url']              = $this->config->wap_base_url.'scenic/order/index.html';
        $data['data']['first']    = array('value' => '恭喜您，付款成功了','color' => '#173177');;
        $data['data']['keyword1'] = array('value' => $payRecord->body,'color' => '#173177');
        $data['data']['keyword2'] = array('value' => $payRecord->pay_id,'color' => '#173177');
        $data['data']['keyword3'] = array('value' => $payRecord->amount,'color' => '#173177');
        $data['data']['keyword4'] = array('value' => date('Y-m-d H:i'),'color' => '#173177');
        $data['data']['remark']   = array('value' => '感谢您的光临，欢迎再次购买！','color' => '#173177');
        $res = json_decode(getApiDynamic(json_encode($data), $urlOne));
        var_dump($res);exit;
        if ($res->errcode != 0) {
            return ;
        }
        return TRUE;
        $msgid = $res->msgid;
        $sendArray = array(
            'ToUserName'    => 'gh_e1b1c43420e9',
            'FromUserName'  => $openId,
            'CreateTime'    => time(),
            'MsgType'       => 'event',
            'Event'         => 'TEMPLATESENDJOBFINISH',
            'MsgID'         => $msgid,
            'Status'        => 'success'
        );
        $xml = $jsapi->arrayToXml($sendArray);
        $jsapi->postXmlCurl($xml, '');
        return FALSE;
    }
    
}